﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Qu.SeniorProject.WebApp.Helpers
{
    public class EnsureHttpsAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            //Verify.NotNull(filterContext, "filterContext");
            //Verify.True(filterContext.HttpContext.Request.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase), "filterContext");

            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            if (!filterContext.HttpContext.Request.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var request = filterContext.HttpContext.Request;
            if (request.Url != null && !request.IsSecureConnection && !request.IsLocal)
                filterContext.Result = new RedirectResult("https://" + request.Url.Host + request.RawUrl);
        }
    }
}